/*
 * Copyright (C) 2013
 * Shopping
 */
package com.mshopping.app.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.http.impl.auth.UnsupportedDigestAlgorithmException;

/**
 * This class is used for ...
 * 
 * @author liuhuan
 * @version 1.0, 2013-9-1 下午7:07:57
 */
public class MD5Encode {

	private static CommonLog log = LogFactory.createLog();
	private static final String ALGORITHM = "MD5";

	private static MessageDigest sDigest;

	static {
		try {
			sDigest = MessageDigest.getInstance(ALGORITHM);
		} catch (NoSuchAlgorithmException e) {
			log.e("Get MD5 Digest failed.");
			throw new UnsupportedDigestAlgorithmException(ALGORITHM, e);
		}
	}

	private MD5Encode() {
	}

	final public static String encode(String source) {
		byte[] btyes = source.getBytes();
		byte[] encodedBytes = sDigest.digest(btyes);

		return Utility.hexString(encodedBytes);
	}

}
